package com.homevip.web;

import com.homevip.core.util.Global;
import com.homevip.core.util.ThirdPart;
import com.homevip.user.service.UserService;
import com.homevip.util.system.CommArray;
import com.homevip.util.system.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;

import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;

/**
 * 
 * 类名称：Initialization <br />
 * 类描述： 初始化系统参数 <br />
 * 
 * @version
 */
@Service
public class Initialization implements ServletContextAware {
	
	ServletContext context;
	@Autowired UserService userService;
	
	public void setServletContext(ServletContext context) {
		this.context = context;
	}

	@PostConstruct
	public void init() {
		initContentPath();
		// 公共数组
		if (null != context){
			context.setAttribute(Const.CONTEXT_COMMARRAY, CommArray.getInstance());
		}
	}

	/**
	 * 初始化项目路径
	 */
	public void initContentPath() {
		
		// 加载全局变量
		if (null == Global.WebPath) Global.load();
		if (null == ThirdPart.ServerInstance) ThirdPart.load();
		
		if (null == context) {

			// 单元测试时
			System.out.println("Initialization not context.");
			return;
		}
		
		// 网站根路径
		String contextPath = context.getContextPath();
		context.setAttribute(Const.CONTEXT_PATH, contextPath);

		// 上传路径
		String uploadPath = contextPath + "/uploads/";
		context.setAttribute(Const.CONTEXT_UPLOADPATH, uploadPath);

		// web路径
		String webPath = Global.WebPath + "/";
		context.setAttribute(Const.CONTEXT_WEBPATH, webPath);
		
		// 平台根路径
		String platformPath = contextPath + "/" + "platform";
		context.setAttribute(Const.CONTEXT_PLATFORMPATH, platformPath);

		// api平台根路径
		String apiPath = contextPath + "/" + "api";
		context.setAttribute(Const.CONTEXT_API, apiPath);
		
		// 微信根路径
		String weixinPath = contextPath + "/" + "weixin";
		context.setAttribute(Const.CONTEXT_WEIXINPATH, weixinPath);

		//是否测试环境
		context.setAttribute("_isTest",ThirdPart.sms_debug);

		//初始管理员
		userService.initAdmin();
	}
}
